package com.app.service.Impl;

import com.app.mapper.GoodsExchangeMapper;
import com.app.mapper.GoodsMapper;
import com.app.mapper.UserIntegralMapper;
import com.app.mapper.UserMapper;
import com.app.model.*;
import com.app.service.GoodsService;
import com.app.util.CommonHelp;
import com.app.vo.GoodsVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private GoodsExchangeMapper goodsExchangeMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserIntegralMapper userIntegralMapper;

    @Override
    public List<SystemModel> selectTypeList() {
        return goodsMapper.selectTypeList();
    }

    @Override
    public GoodsVo selectGoodsList(Integer type, String name, Integer page, Integer limit, Integer villageId) {
        if (page == null || limit == null) {
            return null;
        }
        page = (page - 1) * limit;
        if (name != null && !name.equals("")) {
            List<GoodsModel> goodsModels = goodsMapper.selectGoodsByTitle(name, page, limit, villageId);
            QueryWrapper<GoodsModel> queryWrapper = new QueryWrapper<>();
            queryWrapper.like("name", name);
            if (villageId!=null&&villageId!=0){
                queryWrapper.eq("village_id",villageId);
            }
            Integer count = goodsMapper.selectCount(queryWrapper);
            GoodsVo vo = new GoodsVo();
            vo.setList(goodsModels);
            vo.setCount(count);
            return vo;
        }
        if (type != null && type == 0) {
            List<GoodsModel> goodsModels = goodsMapper.selectGoodsList(page, limit, villageId);
            Integer count;
            if (villageId!=null&&villageId!=0){
                QueryWrapper<GoodsModel> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("village_id",villageId);
                count = goodsMapper.selectCount(queryWrapper);
            }else {
                count = goodsMapper.selectCount(null);
            }
            GoodsVo vo = new GoodsVo();
            vo.setList(goodsModels);
            vo.setCount(count);
            return vo;
        } else {
            List<GoodsModel> goodsModels = goodsMapper.selectGoodsListByType(type, page, limit, villageId);
            QueryWrapper<GoodsModel> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("type", type);
            if (villageId!=null&&villageId!=0){
                queryWrapper.eq("village_id",villageId);
            }
            Integer count = goodsMapper.selectCount(queryWrapper);
            GoodsVo vo = new GoodsVo();
            vo.setList(goodsModels);
            vo.setCount(count);
            return vo;
        }
    }

    @Override
    public GoodsModel selectContentById(Integer id, UserModel user) {
        QueryWrapper<GoodsModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        GoodsModel goodsModel = goodsMapper.selectOne(queryWrapper);
        List<GoodsExchangeModel> goodsExchangeModels = goodsExchangeMapper.selectList(null);
        for (int i = 0; i < goodsExchangeModels.size(); i++) {
            if (goodsExchangeModels.get(i).getStatus() == 0) {
                int num = 0;
                int divide = 0;
                //查询用户待核销商品所有积分
                List<GoodsExchangeModel> integral = goodsExchangeModels.stream()
                        .filter(GoodsModel -> GoodsModel.getUserId().equals(user.getId()))
                        .collect(Collectors.toList());
                for (int i1 = 0; i1 < integral.size(); i1++) {
                    GoodsModel goodsModel1 = goodsMapper.selectById(integral.get(i1).getGoodsId());
                    Integer integral1 = goodsModel1.getIntegral();
                    Integer num1 = integral.get(i1).getNum();
                    divide += integral1 * num1;
                }
                goodsModel.setHandleIntegral(divide);
                //查询该商品待核销所有数量
                List<GoodsExchangeModel> inventory = goodsExchangeModels.stream()
                        .filter(GoodsModel -> GoodsModel.getGoodsId().equals(goodsModel.getId()))
                        .collect(Collectors.toList());
                for (int i1 = 0; i1 < inventory.size(); i1++) {
                    num += inventory.get(i1).getNum();
                }
                goodsModel.setHandleInventory(goodsModel.getInventory() - num);
            }
        }
        return goodsModel;
    }

    @Override
    public GoodsModel selectContentById(Integer id) {
        QueryWrapper<GoodsModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        return goodsMapper.selectOne(queryWrapper);
    }

    @Override
    public void insertGoodsExchange(UserModel user, Integer id, Integer num) {
        QueryWrapper<GoodsModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        GoodsModel goodsModel = goodsMapper.selectOne(queryWrapper);

        GoodsExchangeModel goodsExchangeModel = GoodsExchangeModel.builder()
                .goodsId(id)
                .userId(user.getId())
                .num(num)
                .status(0)
                .integral(goodsModel.getIntegral() * num)
                .createTime(CommonHelp.getNowTimestamp())
                .build();
        goodsExchangeMapper.insert(goodsExchangeModel);

        goodsModel.setInventory(goodsModel.getInventory() - num);
        if (goodsModel.getInventory() != null && goodsModel.getInventory() == 0) {
            goodsModel.setStatus(0);
        }
        goodsModel.setUpdateTime(CommonHelp.getNowTimestamp());
        goodsMapper.updateById(goodsModel);
        user.setIntegral(user.getIntegral() - goodsModel.getIntegral() * num);
        user.setUpdateTime(CommonHelp.getNowTimestamp());


        UserModel userModel = userMapper.selectById(user.getId());
        UserIntegralModel userIntegralModel = new UserIntegralModel();
        userIntegralModel.setUserId(user.getId());
        userIntegralModel.setStatus(0);
        userIntegralModel.setType(3);
        userIntegralModel.setIntegral(goodsModel.getIntegral() * num);
        userIntegralModel.setAssociatedId(goodsExchangeModel.getId());
        userIntegralModel.setCreateTime(CommonHelp.getNowTimestamp());
        if (userModel.getIntegral() < userIntegralModel.getIntegral()) {
            return;
        }
        userIntegralModel.setAfterIntegral(userModel.getIntegral() - userIntegralModel.getIntegral());
        user.setIntegral(userIntegralModel.getAfterIntegral());
        userMapper.updateById(user);
        userIntegralMapper.insert(userIntegralModel);
    }
}
